<?php
/**
 * 
 * Class for generate script tag
 * @author trangvtt
 *
 */

class SGN_StaticResource { 
    const TEST_MODE = 1;
    const DEBUG_MODE = 2;
    
    /**
     * @param $namespace: project for this view
     * @param $view: name of view in the namespace in class namespace format
     * @param $mode: DEV_MODE || TEST_MODE for local environment. DEPLOY_MODE for online enviroment.
     */    
    public static function includeJs($namespace, $view, $mode) {
        if ($mode == 2) {
            $conf = new Zend_Config_Xml(APPLICATION_PATH .  "/configs/$namespace/" . APPLICATION_ENV .".jsInclude.xml", $view, false);
            $array = $conf->toArray();
            $listjs = $array['js'];
            $scr = "";
            foreach ($listjs as $script) {
                $scr = $scr . "<script type='text/javascript' src=" . SGN_Application::get('imgUrl'). '/'.  $script . "> </script>";
            }
            return $scr;
        } else {
        	$conf = new Zend_Config_Xml(APPLICATION_PATH .  "/configs/$namespace/" . APPLICATION_ENV .".fingerprint.xml", $view, false);
        	$array = $conf->toArray();
            $fingerprint = $array[$view];
            return "<script type='text/javascript' src=" . SGN_Application::get('imgUrl'). "/js/compiledjs/" . $view . $fingerprint . ".js> </script>";
        }
    }
    
    /**
     * gateway module related
     * @param unknown_type $host
     */
    public static function getScriptBySubDomain($host) {
    	$conf = SGN_Application::getConfig("sgn", "gateway");
    	$array = $conf->toArray();
    	$list = explode(".",$host);
    	foreach ($list as $value) {
    		$array = $array[$value];
    	}   
    	$returnSet =  array();
    	if ($array['js']) {
    		$returnSet['js'] = SGN_StaticResource::genInput($array['js']);
    	} else {
    		$returnSet['js'] = "none";
    	}
    	
    	if ($array['css']) {
    		$returnSet['css'] = SGN_StaticResource::genInput($array['css']);
    	} else {
    		$returnSet['css'] = "none";
    	}
    	return $returnSet;
    }
    
    public static function genInput($list) {
    	$script= "none";
    	if (is_array($list)) {
    		foreach($list as $value) {
    			$script = $script . "|" . $value;
    		}
    	} else {
    		$script = $script . "|" . $list;
    	}
    	return $script;
    }
}